Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

type-graphql

Package Overview
Dependencies
Maintainers
1
Versions
74
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

type-graphql

Create GraphQL schema and resolvers with TypeScript, using classes and decorators!

  • 1.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
174K
decreased by-11.67%
Maintainers
1
Weekly downloads
 
Created

What is type-graphql?

type-graphql is a library that allows you to create GraphQL APIs using TypeScript. It leverages TypeScript's type system to define the schema and resolvers, making it easier to maintain and understand. The library provides decorators and other utilities to simplify the creation of GraphQL schemas and resolvers.

What are type-graphql's main functionalities?

Schema Definition

This feature allows you to define your GraphQL schema using TypeScript classes and decorators. The example demonstrates how to create a simple object type and a resolver with a query.

const { buildSchema } = require('type-graphql');
const { Resolver, Query, ObjectType, Field } = require('type-graphql');

@ObjectType()
class SampleObject {
  @Field()
  id: number;

  @Field()
  name: string;
}

@Resolver()
class SampleResolver {
  @Query(() => SampleObject)
  sampleQuery() {
    return { id: 1, name: 'Sample' };
  }
}

async function main() {
  const schema = await buildSchema({
    resolvers: [SampleResolver],
  });
  // Use the schema in your GraphQL server setup
}
main();

Resolvers

Resolvers are used to handle the logic for fetching and manipulating data. This example shows how to create a resolver with a query to get users and a mutation to add a user.

const { Resolver, Query, Arg, Mutation } = require('type-graphql');

@Resolver()
class UserResolver {
  private users = [];

  @Query(() => [String])
  getUsers() {
    return this.users;
  }

  @Mutation(() => String)
  addUser(@Arg('name') name: string) {
    this.users.push(name);
    return name;
  }
}

async function main() {
  const schema = await buildSchema({
    resolvers: [UserResolver],
  });
  // Use the schema in your GraphQL server setup
}
main();

Custom Scalars

Custom scalars allow you to define custom data types in your GraphQL schema. This example demonstrates how to create a custom scalar for handling Date objects.

const { Scalar, CustomScalar } = require('type-graphql');
const { GraphQLScalarType, Kind } = require('graphql');

@Scalar('Date')
class DateScalar implements CustomScalar<string, Date> {
  description = 'Date custom scalar type';

  parseValue(value: string): Date {
    return new Date(value); // value from the client input variables
  }

  serialize(value: Date): string {
    return value.toISOString(); // value sent to the client
  }

  parseLiteral(ast: any): Date {
    if (ast.kind === Kind.STRING) {
      return new Date(ast.value); // value from the client query
    }
    return null;
  }
}

async function main() {
  const schema = await buildSchema({
    resolvers: [],
    scalarsMap: [{ type: Date, scalar: DateScalar }],
  });
  // Use the schema in your GraphQL server setup
}
main();

Other packages similar to type-graphql

Keywords

FAQs

Package last updated on 04 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc